﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Linq.Expressions;
using System.Web.Mvc.Html;
using System.Web.Mvc.Ajax;
using System.Web.Routing;


namespace ToyStore.Web.Mvc.Common
{
    public static class HtmlHelpers
    {
            /// <summary>
            /// A Simple ActionLink Image
            /// </summary>
            /// <param name="actionName">name of the action in controller</param>
            /// <param name="imgUrl">url of the image</param>
            /// <param name="alt">alt text of the image</param>
            /// <returns></returns>
        public static MvcHtmlString ImageLink(this HtmlHelper helper, string actionName, string imgUrl, string alt)
            {
                return  ImageLink(helper, actionName, imgUrl, alt, null, null, null);
            }

            /// <summary>
            /// A Simple ActionLink Image
            /// </summary>
            /// <param name="actionName">name of the action in controller</param>
            /// <param name="imgUrl">url of the iamge</param>
            /// <param name="alt">alt text of the image</param>
            /// <returns></returns>
        public static MvcHtmlString ImageLink(this HtmlHelper helper, string actionName, string imgUrl, string alt, object routeValues)
            {
                return ImageLink(helper, actionName, imgUrl, alt, routeValues, null, null);
            }

            /// <summary>
            /// A Simple ActionLink Image
            /// </summary>
            /// <param name="actioNName">name of the action in controller</param>
            /// <param name="imgUrl">url of the image</param>
            /// <param name="alt">alt text of the image</param>
            /// <param name="linkHtmlAttributes">attributes for the link</param>
            /// <param name="imageHtmlAttributes">attributes for the image</param>
            /// <returns></returns>
        public static MvcHtmlString ImageLink(this HtmlHelper helper, string actioNName, string imgUrl, string alt, object routeValues, object linkHtmlAttributes, object imageHtmlAttributes)
            {
                var urlHelper = new UrlHelper(helper.ViewContext.RequestContext);
                var url = urlHelper.Action(actioNName, routeValues);

                //Create the link
                var linkTagBuilder = new TagBuilder("a");
                linkTagBuilder.MergeAttribute("href", url);
                linkTagBuilder.MergeAttributes(AnonymousObjectToHtmlAttributes(linkHtmlAttributes));

                //Create image
                var imageTagBuilder = new TagBuilder("img");
                imageTagBuilder.MergeAttribute("src", urlHelper.Content(imgUrl));
                imageTagBuilder.MergeAttribute("alt", urlHelper.Content(alt));
                imageTagBuilder.MergeAttributes(AnonymousObjectToHtmlAttributes(imageHtmlAttributes));

                //Add image to link
                linkTagBuilder.InnerHtml = imageTagBuilder.ToString(TagRenderMode.SelfClosing);

                return MvcHtmlString.Create(linkTagBuilder.ToString());
            }
        /// <summary>
        /// Podmianka _ na - potrzebne do custom attributes
        /// </summary>
        /// <param name="htmlAttributes"></param>
        /// <returns></returns>
            public static RouteValueDictionary AnonymousObjectToHtmlAttributes(object htmlAttributes)
            {
                RouteValueDictionary result = new RouteValueDictionary();
                if (htmlAttributes != null)
                {
                    foreach (System.ComponentModel.PropertyDescriptor property in System.ComponentModel.TypeDescriptor.GetProperties(htmlAttributes))
                    {
                        result.Add(property.Name.Replace('_', '-'), property.GetValue(htmlAttributes));
                    }
                }
                return result;
            }
        }

    
}